package com.b2b.util;

import com.b2b.model.AdvertisementDto;
import com.b2b.model.entity.Advertisement;
import com.b2b.service.AdvertisementService;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.ArrayList;
import java.util.List;

@Component
public class AdvertisementUtil {

    private final AdvertisementService advertisementService;

    public AdvertisementUtil(AdvertisementService advertisementService){
        this.advertisementService = advertisementService;
    }


    public Integer addAdvertisement(AdvertisementDto advertisementDto){
        Advertisement advertisement = getAdvertisement(advertisementDto);
        Advertisement advertisementTemp = new Advertisement();
        advertisementTemp.setNo(advertisementDto.getNo());
        Integer count = advertisementService.selectCount(advertisementTemp);
        if (count > 0){
            count = -2;

        }else {
            count = advertisementService.insert(advertisement);
            if (count <= 0){
                count = -3;
            }else {
                count = advertisement.getAdvertisementId();
            }
        }
        return count;
//        Advertisement advertisement = getAdvertisement(advertisementDto);
//        Integer result = advertisementService.insert(advertisement);
//        return result;
    }

    public List<Advertisement> getByNo(String[] Nos){
        List<Advertisement> advertisementList = new ArrayList<Advertisement>();
        for (int i = 0; i < Nos.length; i++) {
            Advertisement advertisement = getByNo(Nos[i]);
            advertisementList.add(advertisement);
        }
        return advertisementList;
    }
    public List<Advertisement> selectAdvertisement(){
        List<Advertisement> advertisementList = advertisementService.selectAll();

        return advertisementList;

    }
    public List<Advertisement> getByNoList(String[] noList){
        List<Advertisement> advertisementList = new ArrayList<>();
        for (int i = 0; i < noList.length; i++) {
            String no = noList[i];
            Advertisement advertisement = new Advertisement();
            advertisement.setNo(no);
            advertisement = advertisementService.selectOne(advertisement);
            advertisementList.add(advertisement);
        }
        return advertisementList;
    }
    public Integer updateAdvertisement(AdvertisementDto advertisementDto){
        Integer resuly = advertisementService.updateByPrimaryKeySelective(getAdvertisement(advertisementDto));

        return resuly;
    }
    public Integer deleteAdvertisement(Integer advertisementUtil){
        Advertisement advertisement = new Advertisement();
        advertisement.setAdvertisementId(advertisementUtil);

        Integer result = advertisementService.deleteByPrimaryKey(advertisement);

        return result;
    }

    public Advertisement getByNo(String No){
        Advertisement advertisementTemp = new Advertisement();
        advertisementTemp.setNo(No);
        Advertisement advertisement = advertisementService.selectOne(advertisementTemp);
        return advertisement;
    }
    public Advertisement getAdvertisement(AdvertisementDto advertisementDto){
        Advertisement advertisement = new Advertisement();

        advertisement.setAdvertisementId(advertisementDto.getAdvertisementId());
        advertisement.setImgHref(advertisementDto.getImgHref());
        advertisement.setImgUrl(advertisementDto.getImgUrl());
        advertisement.setNo(advertisementDto.getNo());

        return advertisement;
    }
}
